package net.techwatch.guice;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.google.inject.AbstractModule;
import com.google.inject.name.Names;

public class PropertiesModule extends AbstractModule {

	@Override
	protected void configure() {
		try {
			Properties properties = loadProperties("/hibernate.properties");
			Names.bindProperties(binder(), properties);
		} catch(RuntimeException e) {
			addError("Could not configure hibernate properties", e);
		}
	}
	
	private static Properties loadProperties(String filename) {
		Properties properties = new Properties();
		InputStream is = PropertiesModule.class.getResourceAsStream(filename);
		try {
			properties.load(is);
		} catch(IOException e) {
			throw new RuntimeException(e);
		} finally {
			if(is != null) {
				try {
					is.close();
				} catch(IOException dontCare) { }
			}
		}
		return properties;
	}

}
